package com.wind.cqboot.plugin;

import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.wind.cqboot.utils.HttpUtil;
import com.wind.cqboot.utils.MsgUtil;

import cn.hutool.core.util.StrUtil;
import net.lz1998.pbbot.bot.Bot;
import net.lz1998.pbbot.bot.BotPlugin;
import net.lz1998.pbbot.utils.Msg;
import onebot.OnebotEvent;

/**
 * <p>File：QykRbootPlugin.java</p>
 * <p>Title: 青云机器人api</p>
 * <p>Description:</p>
 * <p>Copyright: Copyright (c) 2020 2020年3月19日 下午1:05:29</p>
 * <p>Company: zmrit.com </p>
 * @author zmr
 * @version 1.0
 */
@Component
public class QykRbootPlugin extends BotPlugin
{
    @Override
    public int onGroupMessage(@NotNull Bot bot,
            @NotNull OnebotEvent.GroupMessageEvent event)
    {
        Long groupId = event.getGroupId();
        Long userId = event.getUserId();
        String atMsg = MsgUtil.getAtMsg(bot, event);
        if (null != atMsg)
        {
            String content = botMessageReply(atMsg);
            Msg.builder().at(userId).text(content).sendToGroup(bot, groupId);
            return MESSAGE_BLOCK;
        }
        // 继续执行下一个插件
        return MESSAGE_IGNORE;
    }

    public String botMessageReply(String msg)
    {
        String params = "key=free&appid=0&msg=" + msg;
        // 只实现了基础的 具体的请自行修改
        return bulid(msg, params);
    }

    private static String bulid(String content, String params)
    {
        String result = HttpUtil.sendGet("http://api.qingyunke.com/api.php",
                params);
        if (StrUtil.isNotBlank(result))
        {
            JsonObject json = JsonParser.parseString(result).getAsJsonObject();
            String text = "";
            if (json.get("result").getAsInt() == 0)
            {
                text = json.get("content").getAsString();
            }
            return text;
        }
        return "机器人暂时失联了，请稍后再试";
    }
}
